//
//  NeedStudyViewController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//


#import "ZJStudySpacialDetailModel.h"
#import "ZJRefurbish.h"
//#import "LPSearchViewController.h"
#import "NeedStudyViewController.h"
/** 未读完视频、音频cell */
#import "ZJNSNnfinishedTableViewCell.h"
/** 未读完图书cell */
#import "ZJUnfinishedBooksTableViewCell.h"
/** 读完图书cell */
#import "ZJFinishedBooksTableViewCell.h"
#import "StudyCoureVideoViewController.h"
#define UnVideoCell  @"ZJNSNnfinishedTableViewCell"
#define UnBookCell   @"ZJUnfinishedBooksTableViewCell"
#define BookCell  @"ZJFinishedBooksTableViewCell"
@interface NeedStudyViewController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 下滑上滑控价 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;
@end

@implementation NeedStudyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(optionRefreshVideoListWithNotification:) name:@"YBRefreshVideoList" object:nil];
    self.view.backgroundColor = [UIColor viewBGGrayColor];
//    [self setNavigViewButton];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    
    [self.tableView.mj_header  beginRefreshing];
    [self setupUI];
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"YBRefreshVideoList" object:nil];
}

//设置导航栏按钮
- (void)setNavigViewButton{
    self.rightButton.hidden = NO;
    //设置搜索按钮
    [self.rightButton setImage:[UIImage imageNamed:@"learn_nav_search"] forState:UIControlStateNormal];
    [self.rightButton setImage:[UIImage imageNamed:@"learn_nav_search"] forState:UIControlStateHighlighted];
    [self.rightButton setTitle:@"搜索" forState:UIControlStateNormal];
    [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:12];
    self.rightButton.titleEdgeInsets = UIEdgeInsetsMake(25, - self.rightButton.imageView.image.size.width, 0.0, 0.0);
    self.rightButton.imageEdgeInsets = UIEdgeInsetsMake(- 15, (self.rightButton.bounds.size.width - self.rightButton.imageView.image.size.width)/2, 0.0,(self.rightButton.bounds.size.width - self.rightButton.imageView.image.size.width)/2);
}

-(void)optionRefreshVideoListWithNotification:(NSNotification *)notification{
    [self networkingForSignDataWithIndex:[notification.userInfo[@"selectedIndex"] integerValue]];
}

- (void)setupUI{
    [self.view addSubview:self.tableView];
}


- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"USER_ID1":[ZJUserInfoModel sharedUserInfo].USER_MODEL_BOOK.ID ? [ZJUserInfoModel sharedUserInfo].USER_MODEL_BOOK.ID:@"",
                           @"USER_ID2":[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.ID,
                           @"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",_page],
                           @"SHOW_NUMBER":@"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL mustLearn] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        NSLog(@"%@" , object);
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJStudySpacialDetailModel class] json:object[@"MUSTSTUDY"]] mutableCopy];
            [weakSelf.tableView reloadData];
            if (weakSelf.dataArray.count == 0) {
                [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:1];
            }
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataArray.count == 0) {
            [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:0];
            [LPShowNoDataManager showNoDataManager].noContentViewTapedBlock = ^{
                [weakSelf.tableView.mj_header beginRefreshing];
            };
        }else{
           [YJProgressHUD showSuccess:@"加载数据失败" inview:weakSelf.view];
        }
    }];

}

//请求单条数据
-(void)networkingForSignDataWithIndex:(NSInteger)index{
    NSDictionary *dict = @{@"USER_ID1":[ZJUserInfoModel sharedUserInfo].USER_MODEL_BOOK.ID ? [ZJUserInfoModel sharedUserInfo].USER_MODEL_BOOK.ID:@"",
                           @"USER_ID2":[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.ID,
                           @"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",index + 1],
                           @"SHOW_NUMBER":@"1"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL mustLearn] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        NSLog(@"%@" , object);
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"MUSTSTUDY"];
            if (array.count > 0) {
                ZJStudySpacialDetailModel * tempModel = [ZJStudySpacialDetailModel yy_modelWithJSON:array.firstObject];
                if (tempModel) {
                    [self.dataArray replaceObjectAtIndex:index withObject:tempModel];
                    [self.tableView reloadData];
                }
            }
            
        }
    } Fail:^(id object) {

    }];
}



- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"USER_ID1":[ZJUserInfoModel sharedUserInfo].USER_MODEL_BOOK.ID? [ZJUserInfoModel sharedUserInfo].USER_MODEL_BOOK.ID:@"",
                           @"USER_ID2":[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.ID,
                           @"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",_page],
                           @"SHOW_NUMBER":@"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL mustLearn] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [NSArray yy_modelArrayWithClass:[ZJStudySpacialDetailModel class] json:object[@"MUSTSTUDY"]];
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}
#pragma mark - 导航栏按钮
- (void)action_leftButton:(UIButton *)sender{
    [[ToolClass getMainNavigationController] popViewControllerAnimated:YES ];
}
//搜索
- (void)action_rightButton:(UIButton *)sender{
//    LPSearchViewController *vc = [LPSearchViewController new];
//    vc.number = 0;
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.dataArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudySpacialDetailModel *model = self.dataArray[indexPath.row];
    
    ZJNSNnfinishedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UnVideoCell forIndexPath:indexPath];
//    ZJFinishedBooksTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BookCell forIndexPath:indexPath];
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 120 * [UIScreen zj_screenHScale];//视频、音频
    return 125 * [UIScreen zj_screenHScale];//图书
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StudyCoureVideoViewController *vc = [StudyCoureVideoViewController new];
    vc.model = self.dataArray[indexPath.row];
    vc.selectedIndex = indexPath.row;
    [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
}

#pragma mark - Setter


#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, navHeight, self.view.width, self.view.height  - navHeight - TabbarHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 7.5 * [UIScreen zj_screenHScale])];
        view.backgroundColor = [UIColor viewBGGrayColor];
        _tableView.tableHeaderView = view;
        [_tableView  registerClass:[ZJNSNnfinishedTableViewCell class] forCellReuseIdentifier: UnVideoCell];
        [_tableView  registerClass:[ZJUnfinishedBooksTableViewCell class] forCellReuseIdentifier: UnBookCell];
        [_tableView  registerClass:[ZJFinishedBooksTableViewCell class] forCellReuseIdentifier: BookCell];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}
@end
